﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Shop : MonoBehaviour {

	public static bool shopInitialized = false;
	string shopItems = System.String.Empty;
	public static string[] shopItemsArray;
	public static int itemCount;
	Transform contentHolder;
	int initialPrice = 100;

	static Shop instance;
	public static Shop Instance
	{
		get
		{
			if(instance == null)
			{
				instance = GameObject.FindObjectOfType(typeof(Shop)) as Shop;
			}

			return instance;
		}
	}

	void Awake () 
	{
		contentHolder = transform.Find("AnimationHolder/Body/ContentHolder/ScrollRect/ScrollObjectHolder");
		if(!shopInitialized)
		{
			InitializeShop();
			shopInitialized = true;
		}
		SetLockedItems();
		transform.Find("AnimationHolder/ButtonFreeCoins/Reward").GetComponent<Text>().text = GlobalVariables.coinsReward.ToString();
	}

	void OnEnable()
	{
		transform.Find("AnimationHolder/CoinsHolder/Coins").GetComponent<Text>().text = GlobalVariables.totalCoins.ToString();
	}

	void InitializeShop()
	{
		if(PlayerPrefs.HasKey("ShopItems"))
		{
			shopItems = PlayerPrefs.GetString("ShopItems");
			string[] tempShopItemsArray = shopItems.Split('#');

			if(contentHolder.childCount < tempShopItemsArray.Length)
			{
				shopItemsArray = new string[contentHolder.childCount];
				GlobalVariables.shopItems = new GlobalVariables.Item[shopItemsArray.Length];
				GlobalVariables.shopItems[0].price = 0;
				GlobalVariables.shopItems[1].price = initialPrice;

				shopItems = tempShopItemsArray[0] + "#" + tempShopItemsArray[1];

				for(int i=0;i<contentHolder.childCount;i++)
				{
					shopItemsArray[i] = tempShopItemsArray[i];
					GlobalVariables.shopItems[i].status = int.Parse(shopItemsArray[i]);
					if(GlobalVariables.shopItems[i].status == 3)
						GlobalVariables.indexOfEquippedItem = i;

					if(i>1)
					{
						GlobalVariables.shopItems[i].price = GlobalVariables.shopItems[i-1].price+(i+1)*25;
						shopItems += "#" + tempShopItemsArray[i];
					}
				}
				PlayerPrefs.SetString("ShopItems",shopItems);
				PlayerPrefs.Save();
			}

			else if(contentHolder.childCount > tempShopItemsArray.Length)
			{
				shopItemsArray = new string[contentHolder.childCount];
				GlobalVariables.shopItems = new GlobalVariables.Item[shopItemsArray.Length];
				GlobalVariables.shopItems[0].price = 0;
				GlobalVariables.shopItems[1].price = initialPrice;

				for(int i=0;i<tempShopItemsArray.Length;i++)
				{
					shopItemsArray[i] = tempShopItemsArray[i];
					GlobalVariables.shopItems[i].status = int.Parse(shopItemsArray[i]);
					if(GlobalVariables.shopItems[i].status == 3)
						GlobalVariables.indexOfEquippedItem = i;

					if(i>1)
						GlobalVariables.shopItems[i].price = GlobalVariables.shopItems[i-1].price+(i+1)*25;
				}

				for(int i=tempShopItemsArray.Length;i<contentHolder.childCount;i++)
				{
					GlobalVariables.shopItems[i].status = 0;
					shopItems += "#0";
					GlobalVariables.shopItems[i].price = GlobalVariables.shopItems[i-1].price+(i+1)*25;
					shopItemsArray[i] = GlobalVariables.shopItems[i].status.ToString();
				}
				PlayerPrefs.SetString("ShopItems",shopItems);
				PlayerPrefs.Save();
			}
			else
			{
				shopItemsArray = new string[tempShopItemsArray.Length];
				GlobalVariables.shopItems = new GlobalVariables.Item[shopItemsArray.Length];
				GlobalVariables.shopItems[0].price = 0;
				GlobalVariables.shopItems[1].price = initialPrice;

				for(int i=0;i<tempShopItemsArray.Length;i++)
				{
					shopItemsArray[i] = tempShopItemsArray[i];
					GlobalVariables.shopItems[i].status = int.Parse(shopItemsArray[i]);
					if(GlobalVariables.shopItems[i].status == 3)
						GlobalVariables.indexOfEquippedItem = i;

					if(i>1)
						GlobalVariables.shopItems[i].price = GlobalVariables.shopItems[i-1].price+(i+1)*25;
				}
			}

		}
		else
		{
			itemCount = contentHolder.childCount;
			GlobalVariables.shopItems = new GlobalVariables.Item[itemCount];
			shopItemsArray = new string[itemCount];
			GlobalVariables.shopItems[0].status = 3;
			GlobalVariables.shopItems[0].price = 0;
			GlobalVariables.shopItems[1].price = initialPrice;
			shopItemsArray[0] = GlobalVariables.shopItems[0].status.ToString();
			shopItems = "3";
			for(int i=1;i<itemCount;i++)
			{
				GlobalVariables.shopItems[i].status = 0;
				shopItems += "#0";
				if(i>1)
					GlobalVariables.shopItems[i].price = GlobalVariables.shopItems[i-1].price+(i+1)*25;
				shopItemsArray[i] = GlobalVariables.shopItems[i].status.ToString();
			}
			PlayerPrefs.SetString("ShopItems",shopItems);
			PlayerPrefs.Save();
		}
	}

	void SetLockedItems()
	{
		for(int i=0;i<contentHolder.childCount;i++)
		{
			if(GlobalVariables.shopItems[i].status != 0)
			{
				contentHolder.GetChild(i).Find("Lock").GetComponent<Image>().enabled = false;
				contentHolder.GetChild(i).Find("Image").GetComponent<Image>().color = Color.white;
			}
		}
	}

	public void SetEquippedItem(Image image)
	{
		if(image != null)
		{
			image.sprite = contentHolder.GetChild(GlobalVariables.indexOfEquippedItem).Find("Image").GetComponent<Image>().sprite;
			image.transform.parent.Find("ImageShadow").GetComponent<Image>().sprite = image.sprite;
		}
	}

	public void BuyItemClicked(RectTransform content)
	{
		int currentIndex = content.GetComponent<ScrollRectSnapController>().currentCharacter;

		if(GlobalVariables.shopItems[currentIndex].price <= GlobalVariables.totalCoins)
		{
			GlobalVariables.totalCoins -= GlobalVariables.shopItems[currentIndex].price;
			transform.Find("AnimationHolder/CoinsHolder/Coins").GetComponent<Text>().text = GlobalVariables.totalCoins.ToString();
			GlobalVariables.shopItems[currentIndex].status = 2;
			shopItemsArray[currentIndex] = GlobalVariables.shopItems[currentIndex].status.ToString();
			shopItems = string.Join("#",shopItemsArray);
			PlayerPrefs.SetInt("TotalCoins",GlobalVariables.totalCoins);
			PlayerPrefs.SetString("ShopItems",shopItems);
			PlayerPrefs.Save();
			GameObject buttonBuy = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
			content.GetChild(currentIndex).Find("Lock").GetComponent<Image>().enabled = false;
			content.GetChild(currentIndex).Find("Image").GetComponent<Image>().color = Color.white;
			buttonBuy.SetActive(false);
			buttonBuy.transform.parent.Find("ButtonEquip").gameObject.SetActive(true);
			buttonBuy.transform.parent.Find("ButtonEquip").GetComponent<Button>().interactable = true;
			GlobalVariables.FlurryEvent("Items Bought Events","Item Bought","Item " + (currentIndex+1));
			SoundManager.Instance.Play_ItemBought();
		}
		else
		{
			//Debug.Log("NEMAS PARE");
			transform.Find("AnimationHolder/CoinsHolder").GetComponent<Animator>().Play("CoinsRed",0,0);
			SoundManager.Instance.Play_Locked();
		}
	}

	public void EquipItem(RectTransform content)
	{
		int currentIndex = content.GetComponent<ScrollRectSnapController>().currentCharacter;
		GlobalVariables.shopItems[currentIndex].status = 3;
		shopItemsArray[currentIndex] = GlobalVariables.shopItems[currentIndex].status.ToString();
		GlobalVariables.shopItems[GlobalVariables.indexOfEquippedItem].status = 2;
		shopItemsArray[GlobalVariables.indexOfEquippedItem] = GlobalVariables.shopItems[GlobalVariables.indexOfEquippedItem].status.ToString();
		GlobalVariables.indexOfEquippedItem = currentIndex;
		shopItems = string.Join("#",shopItemsArray);
		PlayerPrefs.SetString("ShopItems",shopItems);
		PlayerPrefs.Save();
		SoundManager.Instance.Play_ItemEquipped();
	}

	
	public void WatchVideo()
	{
		if (AdsManager.Instance.rewardeVideoId!="0")
		{
			AdsManager.Instance.RewardVideoAd(b =>
			{
				if (b)
				{
					GlobalVariables.AddCoins();
				}
			});
		}
		else
		{
			GlobalVariables.AddCoins();
		}
	}

	public void SetCoinsText()
	{
		transform.Find("AnimationHolder/CoinsHolder/Coins").GetComponent<Text>().text = GlobalVariables.totalCoins.ToString();
		transform.Find("AnimationHolder/CoinsHolder/+CoinsHolder").GetChild(0).GetChild(2).GetComponent<Text>().text = "+" + GlobalVariables.coinsReward;
		transform.Find("AnimationHolder/CoinsHolder/+CoinsHolder").GetChild(0).GetComponent<Animator>().Play("+Coins",0,0);
	}
}
